################################################################################
######################## APPENDIX ROBUSTNESS CHECKS ############################
################################################################################

macro_data <- read_rds("as_macro_data.RData")

library(lpirfs) 
library(tidyverse)
library(stargazer)
library(lmtest)
library(plm)
library(ggpubr)
library(gridExtra)

###### Estimation 

macro_lp <- as.data.frame(macro_data) # define dataset

#### Growth leads ####

lpb1 <- lp_builder(data = macro_lp, panelid = c("country","year"),
                   endog = "socexp_public", shock = "SPENDING",
                   hor = 8, cum_mult = TRUE, vcov_mat = "vcovBK",
                   model = "within", effect = "individual",
                   c_exog = c("realgdpgr","unemp"),
                   lag_exog = c("realgdpgr","unemp"),
                   l_lags = 2, lead_exog = c("realgdpgr"))

lpb2 <- lp_builder(data = macro_lp, panelid = c("country","year"),
                   endog = "socexp_public", shock = "SPENDING",
                   hor = 8, cum_mult = TRUE, vcov_mat = "vcovBK",
                   model = "within", effect = "individual",
                   c_exog = c("realgdpgr","unemp"),
                   lag_exog = c("realgdpgr","unemp","SPENDING"),
                   l_lags = 2, lead_exog = c("SPENDING","realgdpgr"))

p1 <- plot_lp(lpb1, hor = 8, col = "blue", pointest = T, lims = c(-1.75,0.175)) +
  theme(panel.border = element_rect(fill=NA, linewidth=1.2, color="grey"), axis.ticks = element_line(color="grey"))
p2 <- plot_lp(lpb2, hor = 8, col = "blue", pointest = T, ribbon = T, lims = c(-1.75,0.175)) +
  theme(panel.border = element_rect(fill=NA, linewidth=1.2, color="grey"), axis.ticks = element_line(color="grey"))

ggsave(plot = p1, filename = "IRF_FIGA001.png", dpi = 800, height = 3, width = 3.5)
ggsave(plot = p2, filename = "IRF_FIGA01.png", dpi = 800, height = 3, width = 3.5)


##### Two-way fixed effects ######

lpb1 <- lp_builder(data = macro_lp, panelid = c("country","year"),
                   endog = "socexp_public", shock = "SPENDING",
                   hor = 8, cum_mult = TRUE, vcov_mat = "vcovBK",
                   model = "within", effect = "twoways",
                   c_exog = c("realgdpgr","unemp"),
                   lag_exog = c("realgdpgr","unemp"), l_lags = 2)

lpb2 <- lp_builder(data = macro_lp, panelid = c("country","year"),
                   endog = "socexp_public", shock = "SPENDING",
                   hor = 8, cum_mult = T, vcov_mat = "vcovBK",
                   model = "within", effect = "twoways",
                   c_exog = c("realgdpgr","unemp"),
                   lag_exog = c("realgdpgr","unemp","SPENDING"),
                   l_lags = 2, lead_exog = c("SPENDING"))

p1 <- plot_lp(lpb1, hor = 8, col = "blue", pointest = T, lims = c(-1.75,0.05)) +
  theme(panel.border = element_rect(fill=NA, linewidth=1.2, color="grey"), axis.ticks = element_line(color="grey"))
p2 <- plot_lp(lpb2, hor = 8, col = "blue", pointest = T, lims = c(-1.75,0.05)) +
  theme(panel.border = element_rect(fill=NA, linewidth=1.2, color="grey"), axis.ticks = element_line(color="grey"))

ggsave(plot = p1, filename = "IRF_FIGA02.png", dpi = 800, height = 3, width = 3.5)
ggsave(plot = p2, filename = "IRF_FIGA03.png", dpi = 800, height = 3, width = 3.5)


###### Alternative spending clusters: Labor insurance without job creation; R&D and GFCF separately #################

lpb7 <- lp_builder(data = macro_lp, panelid = c("country","year"),
                   endog = "labmarket_spending2", shock = "SPENDING",
                   hor = 8, cum_mult = TRUE, vcov_mat = "vcovBK",
                   model = "within", effect = "individual",
                   c_exog = c("realgdpgr","unemp"),
                   lag_exog = c("realgdpgr","unemp"), l_lags = 2)

lpb8 <- lp_builder(data = macro_lp, panelid = c("country","year"),
                   endog = "rd_exp_public", shock = "SPENDING",
                   hor = 8, cum_mult = TRUE, vcov_mat = "vcovBK",
                   model = "within", effect = "individual",
                   c_exog = c("realgdpgr","unemp"),
                   lag_exog = c("realgdpgr","unemp"), l_lags = 2)

lpb9 <- lp_builder(data = macro_lp, panelid = c("country","year"),
                   endog = "gfcf_exp_public", shock = "SPENDING",
                   hor = 8, cum_mult = TRUE, vcov_mat = "vcovBK",
                   model = "within", effect = "individual",
                   c_exog = c("realgdpgr","unemp"),
                   lag_exog = c("realgdpgr","unemp"), l_lags = 2)

p7 <- plot_lp(lpb7, hor = 8, col = "blue", pointest = T, ribbon = T) +
  theme(panel.border = element_rect(fill=NA, linewidth=1.2, color="grey"), axis.ticks = element_line(color="grey"))
p8 <- plot_lp(lpb8, hor = 8, col = "blue", pointest = T, ribbon = T) +
  theme(panel.border = element_rect(fill=NA, linewidth=1.2, color="grey"), axis.ticks = element_line(color="grey"))
p9 <- plot_lp(lpb9, hor = 8, col = "blue", pointest = T, ribbon = T) +
  theme(panel.border = element_rect(fill=NA, linewidth=1.2, color="grey"), axis.ticks = element_line(color="grey"))

ggsave(plot = p7, filename = "IRF_FIGA04.png", dpi = 800, height = 3, width = 3)
ggsave(plot = p8, filename = "IRF_FIGA05.png", dpi = 800, height = 3, width = 3)
ggsave(plot = p9, filename = "IRF_FIGA06.png", dpi = 800, height = 3, width = 3)


#### Corrected LPs for social consumption & (social) investment #######

lpb1 <- lp_builder(data = macro_lp, panelid = c("country","year"),
                   endog = "cons_spending", shock = "SPENDING",
                   hor = 8, cum_mult = T, vcov_mat = "vcovBK",
                   model = "within", effect = "individual",
                   c_exog = c("realgdpgr","unemp"),
                   lag_exog = c("realgdpgr","unemp","SPENDING"),
                   l_lags = 2, lead_exog = c("SPENDING"))

lpb2 <- lp_builder(data = macro_lp, panelid = c("country","year"),
                   endog = "insurance_spending", shock = "SPENDING",
                   hor = 8, cum_mult = T, vcov_mat = "vcovBK",
                   model = "within", effect = "individual",
                   c_exog = c("realgdpgr","unemp"),
                   lag_exog = c("realgdpgr","unemp","SPENDING"),
                   l_lags = 2, lead_exog = c("SPENDING"))

lpb3 <- lp_builder(data = macro_lp, panelid = c("country","year"),
                   endog = "labmarket_spending", shock = "SPENDING",
                   hor = 8, cum_mult = T, vcov_mat = "vcovBK",
                   model = "within", effect = "individual",
                   c_exog = c("realgdpgr","unemp","elderly"),
                   lag_exog = c("realgdpgr","unemp","SPENDING"),
                   l_lags = 2, lead_exog = c("SPENDING"))

lpb4 <- lp_builder(data = macro_lp, panelid = c("country","year"),
                   endog = "socinvest_spending1", shock = "SPENDING",
                   hor = 8, cum_mult = T, vcov_mat = "vcovBK",
                   model = "within", effect = "individual",
                   c_exog = c("realgdpgr","unemp"),
                   lag_exog = c("realgdpgr","unemp","SPENDING"),
                   l_lags = 2, lead_exog = c("SPENDING"))

lpb5 <- lp_builder(data = macro_lp, panelid = c("country","year"),
                   endog = "socinvest_spending2", shock = "SPENDING",
                   hor = 8, cum_mult = T, vcov_mat = "vcovBK",
                   model = "within", effect = "individual",
                   c_exog = c("realgdpgr","unemp"),
                   lag_exog = c("realgdpgr","unemp","SPENDING"),
                   l_lags = 2, lead_exog = c("SPENDING"))

lpb6 <- lp_builder(data = macro_lp, panelid = c("country","year"),
                   endog = "invest_spending", shock = "SPENDING",
                   hor = 8, cum_mult = T, vcov_mat = "vcovBK",
                   model = "within", effect = "individual",
                   c_exog = c("realgdpgr","unemp"),
                   lag_exog = c("realgdpgr","unemp","SPENDING"),
                   l_lags = 2, lead_exog = c("SPENDING"))

p1 <- plot_lp(lpb1, hor = 8, col = "blue", pointest = T, ribbon = T, lims = c(-0.95,0.25)) +
  theme(panel.border = element_rect(fill=NA, linewidth=1.2, color="grey"), axis.ticks = element_line(color="grey"))
p2 <- plot_lp(lpb2, hor = 8, col = "blue", pointest = T, ribbon = T, lims = c(-0.55,0.25)) +
  theme(panel.border = element_rect(fill=NA, linewidth=1.2, color="grey"), axis.ticks = element_line(color="grey"))
p3 <- plot_lp(lpb3, hor = 8, col = "blue", pointest = T, ribbon = T, lims = c(-0.5,0.25)) +
  theme(panel.border = element_rect(fill=NA, linewidth=1.2, color="grey"), axis.ticks = element_line(color="grey"))
p4 <- plot_lp(lpb4, hor = 8, col = "blue", pointest = T, ribbon = T, lims = c(-0.15,0.1)) +
  theme(panel.border = element_rect(fill=NA, linewidth=1.2, color="grey"), axis.ticks = element_line(color="grey"))
p5 <- plot_lp(lpb5, hor = 8, col = "blue", pointest = T, ribbon = T, lims = c(-0.4,0.15)) +
  theme(panel.border = element_rect(fill=NA, linewidth=1.2, color="grey"), axis.ticks = element_line(color="grey"))
p6 <- plot_lp(lpb6, hor = 8, col = "blue", pointest = T, ribbon = T, lims = c(-0.5,0.25)) +
  theme(panel.border = element_rect(fill=NA, linewidth=1.2, color="grey"), axis.ticks = element_line(color="grey"))

ggsave(plot = p1, filename = "IRF_FIGA07.png", dpi = 800, height = 3, width = 3.5)
ggsave(plot = p2, filename = "IRF_FIGA08.png", dpi = 800, height = 3, width = 3.5)
ggsave(plot = p3, filename = "IRF_FIGA09.png", dpi = 800, height = 3, width = 3.5)
ggsave(plot = p4, filename = "IRF_FIGA10.png", dpi = 800, height = 3, width = 3.5)
ggsave(plot = p5, filename = "IRF_FIGA11.png", dpi = 800, height = 3, width = 3.5)
ggsave(plot = p6, filename = "IRF_FIGA12.png", dpi = 800, height = 3, width = 3.5)


###### GFCF and R&D corrected LPs #######

lpb1 <- lp_builder(data = macro_lp, panelid = c("country","year"),
                   endog = "rd_exp_public", shock = "SPENDING",
                   hor = 8, cum_mult = TRUE, vcov_mat = "vcovBK",
                   model = "within", effect = "individual",
                   c_exog = c("realgdpgr","unemp"),
                   lag_exog = c("realgdpgr","unemp"),
                   l_lags = 2, lead_exog = c("SPENDING"))

lpb2 <- lp_builder(data = macro_lp, panelid = c("country","year"),
                   endog = "gfcf_exp_public", shock = "SPENDING",
                   hor = 8, cum_mult = TRUE, vcov_mat = "vcovBK",
                   model = "within", effect = "individual",
                   c_exog = c("realgdpgr","unemp"),
                   lag_exog = c("realgdpgr","unemp"),
                   l_lags = 2, lead_exog = c("SPENDING"))

p1 <- plot_lp(lpb1, hor = 8, col = "blue", pointest = T, ribbon = T) +
  theme(panel.border = element_rect(fill=NA, linewidth=1.2, color="grey"), axis.ticks = element_line(color="grey"))
p2 <- plot_lp(lpb2, hor = 8, col = "blue", pointest = T, ribbon = T) +
  theme(panel.border = element_rect(fill=NA, linewidth=1.2, color="grey"), axis.ticks = element_line(color="grey"))

ggsave(plot = p1, filename = "IRF_FIGA13.png", dpi = 800, height = 3, width = 3.5)
ggsave(plot = p2, filename = "IRF_FIGA14.png", dpi = 800, height = 3, width = 3.5)

####### Preventive healthcare spending ######

lpb1 <- lp_builder(data = macro_lp, panelid = c("country","year"),
                   endog = "prevhlth_exp_public_ipol",
                   shock = "SPENDING", hor = 8, cum_mult = TRUE,
                   vcov_mat = "vcovBK", model = "within",
                   effect = "individual",
                   c_exog = c("realgdpgr","unemp"),
                   lag_exog = c("realgdpgr","unemp"), l_lags = 2)

p1 <- plot_lp(lpb1, hor = 8, col = "blue", pointest = T, ribbon = T) +
  theme(panel.border = element_rect(fill=NA, linewidth=1.2, color="grey"), axis.ticks = element_line(color="grey"))

ggsave(plot = p1, filename = "IRF_FIGA6.png", dpi = 800, height = 3, width = 3.5)


###### When is retrenchment possible robustness checks ###########

## GFCF State-dependence

lpb1 <- lp_builder(data = macro_lp, panelid = c("country","year"),
                   endog = "invest_spending", shock = "SPENDING",
                   hor = 8, cum_mult = TRUE, vcov_mat = "vcovBK",
                   model = "within", effect = "individual",
                   c_exog = c("realgdpgr","unemp"),
                   lag_exog = c("realgdpgr","unemp"),
                   l_lags = 2, statedep = TRUE, statevar = "fincrisis")

p1 <- plot_lp(lpb1, hor = 8, col1 = NULL, col2 = NULL, pointest = T,
              statedep = TRUE, statelabs = "Financial Crisis",
              ribbon = T, lty1 = NULL, lty2 = NULL, estimat = "within", lims=c(-1,0.75)) +
  theme(panel.border = element_rect(fill=NA, linewidth=1.2, color="grey"), legend.position = "bottom")

ggsave(plot = p1, filename = "IRF_FIGA15.png", dpi = 800, height = 3, width = 2.5)

## Deficit ##

lpb2 <- lp_builder(data = macro_lp, panelid = c("country","year"),
                   endog = "invest_spending", shock = "SPENDING",
                   hor = 8, cum_mult = TRUE, vcov_mat = "vcovBK",
                   model = "within", effect = "individual",
                   c_exog = c("realgdpgr","unemp"),
                   lag_exog = c("realgdpgr","unemp"),
                   l_lags = 2, statedep = TRUE, statevar = "deficit_bin")

p2 <- plot_lp(lpb2, hor = 8, col1 = NULL, col2 = NULL, pointest = T,
              statedep = TRUE, statelabs = "High Deficits",
              ribbon = T, lty1 = NULL, lty2 = NULL, estimat = "within", lims=c(-1,0.75)) + 
  theme(panel.border = element_rect(fill=NA, linewidth=1.2, color="grey"), legend.position = "bottom")

ggsave(plot = p2, filename = "IRF_FIGA16.png", dpi = 800, height = 3, width = 2.5) 

## Slump ##

lpb3 <- lp_builder(data = macro_lp, panelid = c("country","year"),
                   endog = "invest_spending", shock = "SPENDING",
                   hor = 8, cum_mult = TRUE, vcov_mat = "vcovBK",
                   model = "within", effect = "individual",
                   c_exog = c("realgdpgr","unemp"),
                   lag_exog = c("realgdpgr","unemp"),
                   l_lags = 2, statedep = TRUE, statevar = "recession")

p3 <- plot_lp(lpb3, hor = 8, col1 = NULL, col2 = NULL, pointest = T,
              statedep = TRUE, statelabs = "Recession",
              ribbon = T, lty1 = NULL, lty2 = NULL, estimat = "within", lims=c(-1,0.75)) +
  theme(panel.border = element_rect(fill=NA, linewidth=1.2, color="grey"), legend.position = "bottom")

ggsave(plot = p3, filename = "IRF_FIGA17.png", dpi = 800, height = 3, width = 2.5)

pg5 <- grid.arrange(p1,p2,p3,nrow=1)

ggsave(plot = pg5, filename = "IRF_FIGA7.png", dpi = 800, height = 3, width = 7)


# Excluding post-2010 Spain, Portugal, Ireland

macro_lp_npigs1 <- macro_lp %>% dplyr::filter(!((country=="Ireland" | country=="Spain" | country=="Portugal") & year_num>2007)) 

lpb1 <- lp_builder(data = macro_lp_npigs1, panelid = c("country","year"),
                   endog = "socexp_public", shock = "SPENDING",
                   hor = 8, cum_mult = TRUE, vcov_mat = "vcovBK",
                   model = "within", effect = "individual",
                   c_exog = c("realgdpgr","unemp"),
                   lag_exog = c("realgdpgr","unemp"),
                   l_lags = 2, statedep = TRUE, statevar = "fincrisis")

p1 <- plot_lp(lpb1, hor = 8, col1 = NULL, col2 = NULL, pointest = T,
              statedep = TRUE, statelabs = "Financial Crisis",
              ribbon = T, lty1 = NULL, lty2 = NULL, estimat = "within") +
  theme(panel.border = element_rect(fill=NA, linewidth=1.2, color="grey"), legend.position = "bottom")

ggsave(plot = p1, filename = "IRF_FIGA18.png", dpi = 800, height = 3, width = 2.5)


lpb2 <- lp_builder(data = macro_lp_npigs1, panelid = c("country","year"),
                   endog = "socexp_public", shock = "SPENDING", 
                   hor = 8, cum_mult = TRUE, vcov_mat = "vcovBK",
                   model = "within", effect = "individual",
                   c_exog = c("realgdpgr","unemp"), 
                   lag_exog = c("realgdpgr","unemp"),
                   l_lags = 2, statedep = TRUE, statevar = "deficit_bin")

p2 <- plot_lp(lpb2, hor = 8, col1 = NULL, col2 = NULL, pointest = T,
              statedep = TRUE, statelabs = "Deficit Episode", ribbon = T,
              lty1 = NULL, lty2 = NULL, estimat = "within") + 
  theme(panel.border = element_rect(fill=NA, linewidth=1.2, color="grey"), legend.position = "bottom")

ggsave(plot = p2, filename = "IRF_FIGA19.png", dpi = 800, height = 3, width = 2.5)


lpb3 <- lp_builder(data = macro_lp_npigs1, panelid = c("country","year"),
                   endog = "socexp_public", shock = "SPENDING", hor = 8,
                   cum_mult = TRUE, vcov_mat = "vcovBK", model = "within",
                   effect = "individual", c_exog = c("realgdpgr","unemp"),
                   lag_exog = c("realgdpgr","unemp"), l_lags = 2,
                   statedep = TRUE, statevar = "recession")

p3 <- plot_lp(lpb3, hor = 8, col1 = NULL, col2 = NULL, pointest = T,
              statedep = TRUE, statelabs = "Recession", ribbon = T, lty1 = NULL,
              lty2 = NULL, estimat = "within") +
  theme(panel.border = element_rect(fill=NA, linewidth=1.2, color="grey"), legend.position = "bottom")

ggsave(plot = p3, filename = "IRF_FIGA20.png", dpi = 800, height = 3, width = 2.5)


pg6 <- grid.arrange(p1,p2,p3,nrow=1)

ggsave(plot = pg6, filename = "IRF_FIGA8.png", dpi = 800, height = 3, width = 7)



# corrected LPs for state-dependent estimation: 

lpb1 <- lp_builder(data = macro_lp, panelid = c("country","year"),
                   endog = "socexp_public", shock = "SPENDING",
                   hor = 8, cum_mult = TRUE, vcov_mat = "vcovBK",
                   model = "within", effect = "individual",
                   c_exog = c("realgdpgr","unemp"),
                   lag_exog = c("realgdpgr","unemp","SPENDING"),
                   l_lags = 2, lead_exog = c("SPENDING"),
                   statedep = TRUE, statevar = "fincrisis")

p1 <- plot_lp(lpb1, hor = 8, col1 = NULL, col2 = NULL, pointest = T,
              statedep = TRUE, statelabs = "Financial Crisis", ribbon = T, lty1 = NULL, lty2 = NULL, estimat = "within")

## Deficit ##

lpb2 <- lp_builder(data = macro_lp, panelid = c("country","year"),
                   endog = "socexp_public", shock = "SPENDING",
                   hor = 8, cum_mult = TRUE, vcov_mat = "vcovBK",
                   model = "within", effect = "individual",
                   c_exog = c("realgdpgr","unemp"),
                   lag_exog = c("realgdpgr","unemp","SPENDING"),
                   l_lags = 2, lead_exog = c("SPENDING"),
                   statedep = TRUE, statevar = "deficit_bin")

p2 <- plot_lp(lpb2, hor = 8, col1 = NULL, col2 = NULL, pointest = T,
              statedep = TRUE, statelabs = "High Deficits", ribbon = T, lty1 = NULL, lty2 = NULL, estimat = "within")

## Slump ##

lpb3 <- lp_builder(data = macro_lp, panelid = c("country","year"),
                   endog = "socexp_public", shock = "SPENDING", 
                   hor = 8, cum_mult = TRUE, vcov_mat = "vcovBK",
                   model = "within", effect = "individual",
                   c_exog = c("realgdpgr","unemp"),
                   lag_exog = c("realgdpgr","unemp","SPENDING"),
                   l_lags = 2, lead_exog = c("SPENDING"),
                   statedep = TRUE, statevar = "recession")

p3 <- plot_lp(lpb3, hor = 8, col1 = NULL, col2 = NULL, pointest = T,
              statedep = TRUE, statelabs = "Recession", ribbon = T, lty1 = NULL, lty2 = NULL, estimat = "within")



####################### other spending components ###############

lpb1 <- lp_builder(data = macro_lp, panelid = c("country","year"),
                   endog = "unemp_exp_public", shock = "SPENDING",
                   hor = 8, cum_mult = TRUE, vcov_mat = "vcovBK",
                   model = "within", effect = "individual",
                   c_exog = c("realgdpgr","unemp"),
                   lag_exog = c("realgdpgr","unemp"), l_lags = 2)

lpb2 <- lp_builder(data = macro_lp, panelid = c("country","year"),
                   endog = "incapacity_exp_public", shock = "SPENDING",
                   hor = 8, cum_mult = TRUE, vcov_mat = "vcovBK",
                   model = "within", effect = "individual",
                   c_exog = c("realgdpgr","unemp","elderly"),
                   lag_exog = c("realgdpgr","unemp"), l_lags = 2)

lpb3 <- lp_builder(data = macro_lp, panelid = c("country","year"),
                   endog = "survivor_exp_public", shock = "SPENDING",
                   hor = 8, cum_mult = TRUE, vcov_mat = "vcovBK",
                   model = "within", effect = "individual",
                   c_exog = c("realgdpgr","unemp","elderly"),
                   lag_exog = c("realgdpgr","unemp"), l_lags = 2)

lpb4 <- lp_builder(data = macro_lp, panelid = c("country","year"),
                   endog = "family_exp_public_cash", shock = "SPENDING",
                   hor = 8, cum_mult = TRUE, vcov_mat = "vcovBK",
                   model = "within", effect = "individual",
                   c_exog = c("realgdpgr","unemp"),
                   lag_exog = c("realgdpgr","unemp"), l_lags = 2)

lpb5 <- lp_builder(data = macro_lp, panelid = c("country","year"),
                   endog = "othersoc_exp_public", shock = "SPENDING",
                   hor = 8, cum_mult = TRUE, vcov_mat = "vcovBK", 
                   model = "within", effect = "individual",
                   c_exog = c("realgdpgr","unemp"),
                   lag_exog = c("realgdpgr","unemp"), l_lags = 2)

lpb6 <- lp_builder(data = macro_lp, panelid = c("country","year"),
                   endog = "housing_exp_public", shock = "SPENDING",
                   hor = 8, cum_mult = TRUE, vcov_mat = "vcovBK",
                   model = "within", effect = "individual",
                   c_exog = c("realgdpgr","unemp"),
                   lag_exog = c("realgdpgr","unemp"), l_lags = 2)

lpb7 <- lp_builder(data = macro_lp, panelid = c("country","year"),
                   endog = "jobcreation_exp_public", shock = "SPENDING",
                   hor = 8, cum_mult = TRUE, vcov_mat = "vcovBK",
                   model = "within", effect = "individual",
                   c_exog = c("realgdpgr","unemp"),
                   lag_exog = c("realgdpgr","unemp"), l_lags = 2)

lpb8 <- lp_builder(data = macro_lp, panelid = c("country","year"),
                   endog = "almp_exp_public_nojobcrea", shock = "SPENDING",
                   hor = 8, cum_mult = TRUE, vcov_mat = "vcovBK",
                   model = "within", effect = "individual",
                   c_exog = c("realgdpgr","unemp"), 
                   lag_exog = c("realgdpgr","unemp"), l_lags = 2)

p1 <- plot_lp(lpb1, hor = 8, col = "blue", pointest = T, ribbon = T) +
  theme(panel.border = element_rect(fill=NA, linewidth=1.2, color="grey"), axis.ticks = element_line(color="grey"))
p2 <- plot_lp(lpb2, hor = 8, col = "blue", pointest = T, ribbon = T) +
  theme(panel.border = element_rect(fill=NA, linewidth=1.2, color="grey"), axis.ticks = element_line(color="grey"))
p3 <- plot_lp(lpb3, hor = 8, col = "blue", pointest = T, ribbon = T) +
  theme(panel.border = element_rect(fill=NA, linewidth=1.2, color="grey"), axis.ticks = element_line(color="grey"))
p4 <- plot_lp(lpb4, hor = 8, col = "blue", pointest = T, ribbon = T) +
  theme(panel.border = element_rect(fill=NA, linewidth=1.2, color="grey"), axis.ticks = element_line(color="grey"))
p5 <- plot_lp(lpb5, hor = 8, col = "blue", pointest = T, ribbon = T) +
  theme(panel.border = element_rect(fill=NA, linewidth=1.2, color="grey"), axis.ticks = element_line(color="grey"))
p6 <- plot_lp(lpb6, hor = 8, col = "blue", pointest = T, ribbon = T) +
  theme(panel.border = element_rect(fill=NA, linewidth=1.2, color="grey"), axis.ticks = element_line(color="grey"))
p7 <- plot_lp(lpb7, hor = 8, col = "blue", pointest = T, ribbon = T) +
  theme(panel.border = element_rect(fill=NA, linewidth=1.2, color="grey"), axis.ticks = element_line(color="grey"))
p8 <- plot_lp(lpb8, hor = 8, col = "blue", pointest = T, ribbon = T) +
  theme(panel.border = element_rect(fill=NA, linewidth=1.2, color="grey"), axis.ticks = element_line(color="grey"))

ggsave(plot = p1, filename = "IRF_FIG_unemp.png", dpi = 800, height = 2.5, width = 3)
ggsave(plot = p2, filename = "IRF_FIG_incap.png", dpi = 800, height = 2.5, width = 3)
ggsave(plot = p3, filename = "IRF_FIG_surviv.png", dpi = 800, height = 2.5, width = 3)
ggsave(plot = p4, filename = "IRF_FIG_famcash.png", dpi = 800, height = 2.5, width = 3)
ggsave(plot = p5, filename = "IRF_FIG_other.png", dpi = 800, height = 2.5, width = 3)
ggsave(plot = p6, filename = "IRF_FIG_house.png", dpi = 800, height = 2.5, width = 3)
ggsave(plot = p7, filename = "IRF_FIG_jobcrea.png", dpi = 800, height = 2.5, width = 3)
ggsave(plot = p8, filename = "IRF_FIG_almp.png", dpi = 800, height = 2.5, width = 3)


######################### Relative adjustment decompositions ##############################################

# the reference model: 

lpb_b <- lp_builder(data = macro_lp, panelid = c("country","year"),
                    endog = "socexp_public", shock = "SPENDING",
                    hor = 8, cum_mult = TRUE, vcov_mat = "vcovBK",
                    model = "within", effect = "individual",
                    c_exog = c("realgdpgr","unemp"),
                    lag_exog = c("realgdpgr","unemp"),
                    l_lags = 2) 

#### baseline spending categories, type A ####

vlist <- c("cons_spending", "insurance_spending", "labmarket_spending", "socinvest_spending1")

reslist <- list()

for (ii in vlist) {
  
  reslist[[ii]] <- decomposer(mbase = lpb_b, depvar = ii, decomptype = "A")
  
  pl <- ggplot(data = reslist[[ii]], aes(y=(var_df-var_mean),x=Horizon)) +
    geom_hline(yintercept = 0) + geom_bar(stat = "identity", color="red", fill="red") +
    labs(title = "") + ylab("Relative Incidence Type A") +
    theme_pubclean() +
    theme(text = element_text(family = "Times New Roman"),
          plot.title = element_text(hjust = 0.5, size = 11))
  
  wd <- getwd()
  
  exporturl <- paste0(wd, sep="/", "rDA", sep="_", ii, sep=".", "JPEG")
  
  ggsave(pl, filename = exporturl, dpi = 500, height = 2.5, width = 3)
  
}


#### disaggregated spending components #####

vlist <- c("oldage_exp_public","unemp_exp_public","health_exp_public","incapacity_exp_public",
           "survivor_exp_public","family_exp_public_cash","othersoc_exp_public","almp_exp_public_nojobcrea",
           "housing_exp_public","family_inkind_exp_public","jobcreation_exp_public") 

# note that education expenditures are excluded because they are not a component of the social spending multiplier estimated 
# with lpb_b

reslist <- list()

for (ii in vlist) {
  
  reslist[[ii]] <- decomposer(mbase = lpb_b, depvar = ii, decomptype = "A")
  
  pl <- ggplot(data = reslist[[ii]], aes(y=(var_df-var_mean),x=Horizon)) +
    geom_hline(yintercept = 0) + geom_bar(stat = "identity", color="red", fill="red") +
    labs(title = "") + ylab("Relative Incidence Type A") +
    theme_pubclean() +
    theme(text = element_text(family = "Times New Roman"),
          plot.title = element_text(hjust = 0.5, size = 11))
  
  wd <- getwd()
  
  exporturl <- paste0(wd, sep="/", "rDA", sep="_", ii, sep=".", "JPEG")
  
  ggsave(pl, filename = exporturl, dpi = 500, height = 2.5, width = 3)
  
}

vlist <- c("oldage_exp_public","unemp_exp_public","health_exp_public","incapacity_exp_public",
           "survivor_exp_public","family_exp_public_cash","othersoc_exp_public","almp_exp_public_nojobcrea",
           "housing_exp_public","educexp_public_ipol","family_inkind_exp_public","jobcreation_exp_public",
           "gfcf_exp_public","rd_exp_public")

reslist <- list()

for (ii in vlist) {
  
  reslist[[ii]] <- decomposer(mbase = lpb_b, depvar = ii, decomptype = "B")
  
  pl <- ggplot(data = reslist[[ii]], aes(y=var_df*100,x=Horizon)) +
    geom_hline(yintercept = 0) + geom_bar(stat = "identity", color="red", fill="red") +
    labs(title = "") + ylab("Relative Incidence Type B, %") +
    theme_pubclean() +
    theme(text = element_text(family = "Times New Roman"),
          plot.title = element_text(hjust = 0.5, size = 11))
  
  wd <- getwd()
  
  exporturl <- paste0(wd, sep="/", "rDB", sep="_", ii, sep=".", "JPEG")
  
  ggsave(pl, filename = exporturl, dpi = 500, height = 2.5, width = 3)
  
}


########## autoregressions #############

macro_ardl <- macro_lp

macro_ardl <- macro_ardl %>%
  group_by(country) %>%
  mutate(socexp_public_fd = socexp_public - dplyr::lag(socexp_public, n=1L),
         unemp_fd = unemp - dplyr::lag(unemp, 1)) %>%
  ungroup()

macro_ardl <- pdata.frame(macro_ardl, index = c("country","year"))

library(ARDL)

adl1 <- ardl(formula = socexp_public_fd ~ SPENDING + realgdpgr + unemp_fd, data = macro_ardl, order = c(1,1,1,1))
adl2 <- ardl(formula = socexp_public_fd ~ SPENDING + realgdpgr + unemp_fd, data = macro_ardl, order = c(1,2,2,2))
adl3 <- ardl(formula = socexp_public_fd ~ SPENDING + realgdpgr + unemp_fd, data = macro_ardl, order = c(1,3,3,3))
adl4 <- ardl(formula = socexp_public_fd ~ SPENDING + realgdpgr + unemp_fd, data = macro_ardl, order = c(1,4,4,4))

summary(adl1)
summary(adl2)
summary(adl3)
summary(adl4)

mu1 <- multipliers(adl1, type = "lr", vcov_matrix = vcovHAC(adl1))
mu2 <- multipliers(adl2, type = "lr", vcov_matrix = vcovHAC(adl2))
mu3 <- multipliers(adl3, type = "lr", vcov_matrix = vcovHAC(adl3))
mu4 <- multipliers(adl4, type = "lr", vcov_matrix = vcovHAC(adl4))

mu1$lagord <- "(1,1,1,1)"
mu2$lagord <- "(1,2,2,2)"
mu3$lagord <- "(1,3,3,3)"
mu4$lagord <- "(1,4,4,4)"

mudf <- as.data.frame(rbind(mu1,mu2,mu3,mu4))

mudf <- mudf %>% dplyr::filter(term=="SPENDING")

p1 <- ggplot(data=mudf, aes(y=estimate, x=lagord)) + geom_hline(yintercept = 0, linetype=2, alpha=0.75) + geom_point(aes(y=estimate), color="blue3") + geom_errorbar(aes(ymax=estimate+std.error*1.96, ymin=estimate-std.error*1.96), color="blue3", width=0) + ylab("LRM") + xlab("Lag Order") + theme_pubclean() + theme(text=element_text(family="Tims"))

ggsave(plot = p1, filename = "IRF_FIGA002.png", dpi = 800, height = 3, width = 3.5)

#### CMP EVIDENCE ON SALIENCE

macro_data$country_fac <- as.factor(macro_data$country)
macro_data$year_fac <- as.factor(macro_data$year)

m1 <- lm(econorth_wgts_lpol ~ deficit + unemp + realgdpgr + country_fac, data=macro_data)
m2 <- lm(econorth_wgts ~ deficit + unemp + realgdpgr + country_fac, data=macro_data)
m3 <- lm(econorth_lpol ~ deficit + unemp + realgdpgr + country_fac, data=macro_data)
m4 <- lm(econorth ~ deficit + unemp + realgdpgr + country_fac, data=macro_data)

stargazer(m1,m2,m3,m4,type="text",omit = c("country_fac"))

m5 <- lm(welfare_wgts_lpol ~ deficit + unemp + realgdpgr + country_fac, data=macro_data)
m6 <- lm(welfare_wgts ~ deficit + unemp + realgdpgr + country_fac, data=macro_data)
m7 <- lm(welfare_lpol ~ deficit + unemp + realgdpgr + country_fac, data=macro_data)
m8 <- lm(welfare ~ deficit + unemp + realgdpgr + country_fac, data=macro_data)

stargazer(m5,m6,m7,m8,type="text",omit = c("country_fac"))

stargazer(m1,m2,m3,m4,type="latex",omit = c("country_fac"))
stargazer(m5,m6,m7,m8,type="latex",omit = c("country_fac"))



##### JACKKNIFE TESTS ######

jackdata <- macro_lp %>% dplyr::filter(!country=="Australia")

lpb1 <- lp_builder(data = jackdata, panelid = c("country","year"), endog = "socexp_public",
                   shock = "SPENDING", hor = 8, cum_mult = TRUE, vcov_mat = "vcovBK",
                   model = "within", effect = "individual", c_exog = c("realgdpgr","unemp"), lag_exog = c("realgdpgr","unemp"), l_lags = 2)

plot_lp(lpb1, hor = 8, col = "blue", pointest = T, ribbon = T, lims = c(-1.9,0.175)) +
  theme(panel.border = element_rect(fill=NA, linewidth=1.2, color="grey"), axis.ticks = element_line(color="grey"))


jackdata <- macro_lp %>% dplyr::filter(!country=="Austria")

lpb1 <- lp_builder(data = jackdata, panelid = c("country","year"), endog = "socexp_public",
                   shock = "SPENDING", hor = 8, cum_mult = TRUE, vcov_mat = "vcovBK",
                   model = "within", effect = "individual", c_exog = c("realgdpgr","unemp"), lag_exog = c("realgdpgr","unemp"), l_lags = 2)

plot_lp(lpb1, hor = 8, col = "blue", pointest = T, ribbon = T, lims = c(-1.9,0.175)) +
  theme(panel.border = element_rect(fill=NA, linewidth=1.2, color="grey"), axis.ticks = element_line(color="grey"))


jackdata <- macro_lp %>% dplyr::filter(!country=="Belgium")

lpb1 <- lp_builder(data = jackdata, panelid = c("country","year"), endog = "socexp_public",
                   shock = "SPENDING", hor = 8, cum_mult = TRUE, vcov_mat = "vcovBK",
                   model = "within", effect = "individual", c_exog = c("realgdpgr","unemp"), lag_exog = c("realgdpgr","unemp"), l_lags = 2)

plot_lp(lpb1, hor = 8, col = "blue", pointest = T, ribbon = T, lims = c(-1.9,0.175)) +
  theme(panel.border = element_rect(fill=NA, linewidth=1.2, color="grey"), axis.ticks = element_line(color="grey"))


jackdata <- macro_lp %>% dplyr::filter(!country=="Canada")

lpb1 <- lp_builder(data = jackdata, panelid = c("country","year"), endog = "socexp_public",
                   shock = "SPENDING", hor = 8, cum_mult = TRUE, vcov_mat = "vcovBK",
                   model = "within", effect = "individual", c_exog = c("realgdpgr","unemp"), lag_exog = c("realgdpgr","unemp"), l_lags = 2)

plot_lp(lpb1, hor = 8, col = "blue", pointest = T, ribbon = T, lims = c(-1.9,0.175)) +
  theme(panel.border = element_rect(fill=NA, linewidth=1.2, color="grey"), axis.ticks = element_line(color="grey"))


jackdata <- macro_lp %>% dplyr::filter(!country=="Denmark")

lpb1 <- lp_builder(data = jackdata, panelid = c("country","year"), endog = "socexp_public",
                   shock = "SPENDING", hor = 8, cum_mult = TRUE, vcov_mat = "vcovBK",
                   model = "within", effect = "individual", c_exog = c("realgdpgr","unemp"), lag_exog = c("realgdpgr","unemp"), l_lags = 2)

plot_lp(lpb1, hor = 8, col = "blue", pointest = T, ribbon = T, lims = c(-1.9,0.175)) +
  theme(panel.border = element_rect(fill=NA, linewidth=1.2, color="grey"), axis.ticks = element_line(color="grey"))


jackdata <- macro_lp %>% dplyr::filter(!country=="Finland")

lpb1 <- lp_builder(data = jackdata, panelid = c("country","year"), endog = "socexp_public",
                   shock = "SPENDING", hor = 8, cum_mult = TRUE, vcov_mat = "vcovBK",
                   model = "within", effect = "individual", c_exog = c("realgdpgr","unemp"), lag_exog = c("realgdpgr","unemp"), l_lags = 2)

plot_lp(lpb1, hor = 8, col = "blue", pointest = T, ribbon = T, lims = c(-1.9,0.175)) +
  theme(panel.border = element_rect(fill=NA, linewidth=1.2, color="grey"), axis.ticks = element_line(color="grey"))


jackdata <- macro_lp %>% dplyr::filter(!country=="France")

lpb1 <- lp_builder(data = jackdata, panelid = c("country","year"), endog = "socexp_public",
                   shock = "SPENDING", hor = 8, cum_mult = TRUE, vcov_mat = "vcovBK",
                   model = "within", effect = "individual", c_exog = c("realgdpgr","unemp"), lag_exog = c("realgdpgr","unemp"), l_lags = 2)

plot_lp(lpb1, hor = 8, col = "blue", pointest = T, ribbon = T, lims = c(-1.9,0.175)) +
  theme(panel.border = element_rect(fill=NA, linewidth=1.2, color="grey"), axis.ticks = element_line(color="grey"))


jackdata <- macro_lp %>% dplyr::filter(!country=="Germany")

lpb1 <- lp_builder(data = jackdata, panelid = c("country","year"), endog = "socexp_public",
                   shock = "SPENDING", hor = 8, cum_mult = TRUE, vcov_mat = "vcovBK",
                   model = "within", effect = "individual", c_exog = c("realgdpgr","unemp"), lag_exog = c("realgdpgr","unemp"), l_lags = 2)

plot_lp(lpb1, hor = 8, col = "blue", pointest = T, ribbon = T, lims = c(-1.9,0.175)) +
  theme(panel.border = element_rect(fill=NA, linewidth=1.2, color="grey"), axis.ticks = element_line(color="grey"))


jackdata <- macro_lp %>% dplyr::filter(!country=="Ireland")

lpb1 <- lp_builder(data = jackdata, panelid = c("country","year"), endog = "socexp_public",
                   shock = "SPENDING", hor = 8, cum_mult = TRUE, vcov_mat = "vcovBK",
                   model = "within", effect = "individual", c_exog = c("realgdpgr","unemp"), lag_exog = c("realgdpgr","unemp"), l_lags = 2)

plot_lp(lpb1, hor = 8, col = "blue", pointest = T, ribbon = T, lims = c(-1.9,0.175)) +
  theme(panel.border = element_rect(fill=NA, linewidth=1.2, color="grey"), axis.ticks = element_line(color="grey"))


jackdata <- macro_lp %>% dplyr::filter(!country=="Italy")

lpb1 <- lp_builder(data = jackdata, panelid = c("country","year"), endog = "socexp_public",
                   shock = "SPENDING", hor = 8, cum_mult = TRUE, vcov_mat = "vcovBK",
                   model = "within", effect = "individual", c_exog = c("realgdpgr","unemp"), lag_exog = c("realgdpgr","unemp"), l_lags = 2)

plot_lp(lpb1, hor = 8, col = "blue", pointest = T, ribbon = T, lims = c(-1.9,0.175)) +
  theme(panel.border = element_rect(fill=NA, linewidth=1.2, color="grey"), axis.ticks = element_line(color="grey"))

jackdata <- macro_lp %>% dplyr::filter(!country=="Japan")

lpb1 <- lp_builder(data = jackdata, panelid = c("country","year"), endog = "socexp_public",
                   shock = "SPENDING", hor = 8, cum_mult = TRUE, vcov_mat = "vcovBK",
                   model = "within", effect = "individual", c_exog = c("realgdpgr","unemp"), lag_exog = c("realgdpgr","unemp"), l_lags = 2)

plot_lp(lpb1, hor = 8, col = "blue", pointest = T, ribbon = T, lims = c(-1.9,0.175)) +
  theme(panel.border = element_rect(fill=NA, linewidth=1.2, color="grey"), axis.ticks = element_line(color="grey"))


jackdata <- macro_lp %>% dplyr::filter(!country=="Portugal")

lpb1 <- lp_builder(data = jackdata, panelid = c("country","year"), endog = "socexp_public",
                   shock = "SPENDING", hor = 8, cum_mult = TRUE, vcov_mat = "vcovBK",
                   model = "within", effect = "individual", c_exog = c("realgdpgr","unemp"), lag_exog = c("realgdpgr","unemp"), l_lags = 2)

plot_lp(lpb1, hor = 8, col = "blue", pointest = T, ribbon = T, lims = c(-1.9,0.175)) +
  theme(panel.border = element_rect(fill=NA, linewidth=1.2, color="grey"), axis.ticks = element_line(color="grey"))


jackdata <- macro_lp %>% dplyr::filter(!country=="Spain")

lpb1 <- lp_builder(data = jackdata, panelid = c("country","year"), endog = "socexp_public",
                   shock = "SPENDING", hor = 8, cum_mult = TRUE, vcov_mat = "vcovBK",
                   model = "within", effect = "individual", c_exog = c("realgdpgr","unemp"), lag_exog = c("realgdpgr","unemp"), l_lags = 2)

plot_lp(lpb1, hor = 8, col = "blue", pointest = T, ribbon = T, lims = c(-1.9,0.175)) +
  theme(panel.border = element_rect(fill=NA, linewidth=1.2, color="grey"), axis.ticks = element_line(color="grey"))


jackdata <- macro_lp %>% dplyr::filter(!country=="Sweden")

lpb1 <- lp_builder(data = jackdata, panelid = c("country","year"), endog = "socexp_public",
                   shock = "SPENDING", hor = 8, cum_mult = TRUE, vcov_mat = "vcovBK",
                   model = "within", effect = "individual", c_exog = c("realgdpgr","unemp"), lag_exog = c("realgdpgr","unemp"), l_lags = 2)

plot_lp(lpb1, hor = 8, col = "blue", pointest = T, ribbon = T, lims = c(-1.9,0.175)) +
  theme(panel.border = element_rect(fill=NA, linewidth=1.2, color="grey"), axis.ticks = element_line(color="grey"))


jackdata <- macro_lp %>% dplyr::filter(!country=="United Kingdom")

lpb1 <- lp_builder(data = jackdata, panelid = c("country","year"), endog = "socexp_public",
                   shock = "SPENDING", hor = 8, cum_mult = TRUE, vcov_mat = "vcovBK",
                   model = "within", effect = "individual", c_exog = c("realgdpgr","unemp"), lag_exog = c("realgdpgr","unemp"), l_lags = 2)

plot_lp(lpb1, hor = 8, col = "blue", pointest = T, ribbon = T, lims = c(-1.9,0.175)) +
  theme(panel.border = element_rect(fill=NA, linewidth=1.2, color="grey"), axis.ticks = element_line(color="grey"))


jackdata <- macro_lp %>% dplyr::filter(!country=="USA")

lpb1 <- lp_builder(data = jackdata, panelid = c("country","year"), endog = "socexp_public",
                   shock = "SPENDING", hor = 8, cum_mult = TRUE, vcov_mat = "vcovBK",
                   model = "within", effect = "individual", c_exog = c("realgdpgr","unemp"), lag_exog = c("realgdpgr","unemp"), l_lags = 2)

plot_lp(lpb1, hor = 8, col = "blue", pointest = T, ribbon = T, lims = c(-1.9,0.175)) +
  theme(panel.border = element_rect(fill=NA, linewidth=1.2, color="grey"), axis.ticks = element_line(color="grey"))



#### single-country cases ###

jackdata <- macro_lp %>% dplyr::filter(country=="Sweden"|country=="Finland"|country=="Denmark")

lpb1 <- lp_builder(data = jackdata, panelid = c("country","year"), endog = "socexp_public",
                   shock = "SPENDING", hor = 8, cum_mult = TRUE, vcov_mat = "vcovBK", model = "within",
                   effect = "individual", c_exog = c("realgdpgr","unemp"), lag_exog = c("realgdpgr","unemp"), l_lags = 2)

plot_lp(lpb1, hor = 8, col = "blue", pointest = T, ribbon = T, lims = c(-2.5,1)) +
  theme(panel.border = element_rect(fill=NA, linewidth=1.2, color="grey"), axis.ticks = element_line(color="grey"))





